package com.zmn.oms.zmn.business.impl.pricefeedback;

import com.alibaba.fastjson.JSON;
import com.zmn.base.common.data.common.dro.area.AreaDRO;
import com.zmn.base.common.data.dubbo.interfaces.area.AreaListRemoteService;
import com.zmn.base.engineer.common.constants.DubboConsts;
import com.zmn.base.price.common.dro.item.ItemAdditionDRO;
import com.zmn.base.price.common.dro.item.ItemDRO2;
import com.zmn.base.price.common.dro.tariff.TariffDRO;
import com.zmn.base.price.dubbo.interfaces.item.ItemAdditionRemoteService;
import com.zmn.base.price.dubbo.interfaces.item.ItemRemoteService;
import com.zmn.base.price.dubbo.interfaces.tariff.TariffRemoteService;
import com.zmn.base.product.common.consts.BaseProductConsts;
import com.zmn.base.product.common.dto.product.ProductBaseQuery;
import com.zmn.base.product.common.dto.product.ec.ProductBaseDRO;
import com.zmn.base.product.dubbo.interfaces.product.foreign.ProductForeignListRemoteService;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.consts.GlobalConsts;
import com.zmn.oms.common.dio.pricefeedback.PriceFeedbackDIO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.entity.pricefeedback.PriceFeedback;
import com.zmn.oms.model.entity.product.OrderProduct;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.services.interfaces.pricefeedback.PriceFeedbackService;
import com.zmn.oms.services.interfaces.product.OrderProductService;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.zmn.business.interfaces.pricefeedback.PriceFeedbackBService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 类描述: 工程师反馈价格业务接口类
 *
 * @author weilai
 * @since 2022/9/7  14:25
 */
@Slf4j
@Service
public class PriceFeedbackBServiceImpl implements PriceFeedbackBService {

    private static final String TAG = "反馈价格业务接口类";

    @Resource
    private OrderWorkService orderWorkService;

    @Resource
    private PriceFeedbackService priceFeedbackService;

    @Resource
    private OrderProductService orderProductService;

    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private AreaListRemoteService areaListRemoteService;

    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private ItemRemoteService itemRemoteService;

    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private ProductForeignListRemoteService productForeignListRemoteService;

    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private TariffRemoteService tariffRemoteService;

    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private ItemAdditionRemoteService itemAdditionRemoteService;


    @Override
    public void addPriceFeedback(PriceFeedbackDIO priceFeedbackDIO) throws OmsBaseException {
        OrderWork orderWork = orderWorkService.findOrderWorkByKey(priceFeedbackDIO.getOrderId(), priceFeedbackDIO.getWorkId());
        if (Objects.isNull(orderWork)) {
            log.error("[{}],工单Id:{},未查询到工单", TAG, orderWork.getOrderId());
            throw new OmsBaseException("未查询到工单");
        }

        // 查询城市
        ResponseDTO<AreaDRO> areaDRO = areaListRemoteService.getCityAndProvinceByCityId(orderWork.getCityId());
        log.info("[{}],工单Id:{},查询城市,入参:{},出参:{}", TAG, orderWork.getOrderId(), orderWork.getCityId(), JSON.toJSONString(areaDRO));
        if (!areaDRO.isSuccess() || Objects.isNull(areaDRO.getData())) {
            log.error("[{}],工单Id:{},查询城市异常", TAG, orderWork.getOrderId());
            throw new OmsBaseException("查询城市异常");
        }

        // 查询工单产品
        List<OrderProduct> orderProducts = orderProductService.listOrderProductByOrderId(priceFeedbackDIO.getOrderId());
        if (CollectionUtil.isNullOrEmpty(orderProducts)) {
            log.error("[{}],工单Id:{},未查询到工单产品");
            throw new OmsBaseException("查询工单产品异常");
        }
        OrderProduct orderProduct = orderProducts.get(0);

        // 查询项目
        String itemName = "";
        ResponseDTO<ItemDRO2> itemDRO = itemRemoteService.getById(priceFeedbackDIO.getProjectId());
        log.info("[{}],工单Id:{},查询服务项,入参:{},出参:{}", TAG, orderWork.getOrderId(), priceFeedbackDIO.getProjectId(), JSON.toJSONString(itemDRO));
        if (!itemDRO.isSuccess() || Objects.isNull(itemDRO.getData())) {
            log.error("[{}],工单Id:{},查询服务项异常", TAG, orderWork.getOrderId());
            // 查询不到服务项目 则查询附加项
            ResponseDTO<ItemAdditionDRO> itemAdditionDRO = itemAdditionRemoteService.getById(priceFeedbackDIO.getProjectId());
            if (!itemAdditionDRO.isSuccess() || Objects.isNull(itemAdditionDRO.getData())) {
                log.error("[{}],工单Id:{},查询附加项异常", TAG, orderWork.getOrderId());
                throw new OmsBaseException("查询服务项异常");
            }else {
                itemName = itemAdditionDRO.getData().getItemAdditionName();
            }
        }else {
            itemName = itemDRO.getData().getItemName();
        }
        // 查询产品
        ProductBaseQuery productBaseQuery = ProductBaseQuery.builder()
                .productId(orderWork.getProductId())
                .showType(BaseProductConsts.ERP_SHOW_TYPE).build();
        ResponseDTO<List<ProductBaseDRO>> productBaseDROS = productForeignListRemoteService.listBaseDROByQuery(productBaseQuery);
        log.info("[{}],工单Id:{},查询后台产品,入参:{},出参:{}", TAG, orderWork.getOrderId(), JSON.toJSONString(productBaseQuery), JSON.toJSONString(productBaseDROS));
        if (!productBaseDROS.isSuccess() || CollectionUtil.isNullOrEmpty(productBaseDROS.getData())) {
            log.error("[{}],工单Id:{},查询后台产品异常", TAG, orderWork.getOrderId());
            throw new OmsBaseException("查询后台产品异常");
        }
        ProductBaseDRO productBaseDRO = productBaseDROS.getData().get(0);

        // 查询价格表
        ResponseDTO<TariffDRO> tariffDRO = tariffRemoteService.getById(orderProduct.getTariffId());
        log.info("[{}],工单Id:{},查询价格表,入参:{},出参:{}", TAG, orderWork.getOrderId(), orderProduct.getTariffId(), JSON.toJSONString(tariffDRO));
        if (!tariffDRO.isSuccess() || Objects.isNull(tariffDRO.getData())) {
            log.error("[{}],工单Id:{},查询价格表异常", TAG, orderWork.getOrderId());
            throw new OmsBaseException("查询价格表异常");
        }

        // 组装数据
        PriceFeedback priceFeedback = PriceFeedback.builder()
                .orderId(orderWork.getOrderId())
                .engineerId(orderWork.getMasterId())
                .cityId(orderWork.getCityId())
                .cityName(areaDRO.getData().getCityName())
                .channelName(orderWork.getChannelName())
                .channelId(orderWork.getChannelId())
                .bizType(orderWork.getBizType())
                .servCategId(orderWork.getServCategId())
                .servCateg(orderWork.getShowServCategName())
                .categOneId(orderWork.getShowCategOneId())
                .categone(orderProduct.getShowCategOneName())
                .categTwoId(orderWork.getShowCategId())
                .categTwo(orderProduct.getShowCategName())
                .productName(orderProduct.getShowProductName())
                .productId(orderWork.getShowProductId())
                .erpServCategId(productBaseDRO.getServCategId())
                .erpServCateg(productBaseDRO.getServCategName())
                .erpCategOneId(productBaseDRO.getCategOneId())
                .erpCategOne(productBaseDRO.getCategOneName())
                .erpCategTwoId(productBaseDRO.getCategId())
                .erpCategTwo(productBaseDRO.getCategName())
                .erpProductName(productBaseDRO.getProductName())
                .projectName(itemName)
                .projectId(priceFeedbackDIO.getProjectId())
                .productType(Objects.equals(productBaseDRO.getProductType(), GlobalConsts.ONE) ? "通用产品" : "渠道产品")
                .brandId(orderProduct.getBrandId())
                .brand(orderProduct.getBrandName())
                .tariffId(orderProduct.getTariffId())
                .tariffName(tariffDRO.getData().getTariffName())
                .tariffCalMode(Objects.equals(tariffDRO.getData().getTariffCalMode(), GlobalConsts.ONE) ? "系数定价" : "城市定价")
                .feedbackPrice(priceFeedbackDIO.getFeedbackPrice())
                .expectPrice(priceFeedbackDIO.getExpectPrice())
                .remark(priceFeedbackDIO.getRemark())
                .erpProductId(orderWork.getProductId())
                .build();
        Integer insertPriceFeedback = priceFeedbackService.insert(priceFeedback);
        if (insertPriceFeedback == 0) {
            throw new OmsBaseException("录入价格反馈失败");
        }
    }

}
